import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Home',
        redirect: '/welcome',
        component: () => import('../views/Home.vue'),
        children: [{
            path: '/about',
            name: 'About',
            component: () => import('../views/About.vue')
        }, {
            path: '/welcome',
            name: 'Welcome',
            component: () => import('../views/Welcome.vue')
        }, {
            path: '/user',
            name: 'User',
            component: () => import('../views/user/User.vue')
        }, {
            path: '/nextTickDemo',
            name: 'nextTickDemo',
            component: () => import('../views/nextTickDemo.vue')
        }, ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/Login.vue'),
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    let isLogin = sessionStorage.getItem("login")
    console.log(`isLogin的值是：${isLogin}`)
    if (to.path === '/login') return next()

    if (isLogin !== "yes") {
        console.log(isLogin === 'yes')
        return next('/login')
    } else {
        next()
    }

})

export default router